package com.xiyu.service.service.area;

import com.xiyu.service.vo.area.officeArea.OfficeAreaSimpleListOutput;
import com.xiyu.service.vo.area.officeArea.OfficeAreaSingleGetOutput;
import com.xiyu.service.vo.area.officeArea.OfficeAreaUpdateInput;
import com.xiyu.service.vo.area.officeArea.OfficeAreaCreateInput;
import com.xiyu.service.model.office.area.OfficeArea;
import com.xiyu.service.repository.office.area.OfficeAreaRepository;
import com.xiyu.service.vo.area.officeArea.OfficeAreaPageQueryOutput;
import com.xiyu.service.vo.area.officeArea.OfficeAreaPageQueryInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.util.entity.EntityUtils;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.area.OfficeAreaConvert;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.area.OfficeAreaErrorCode.*;

/**
 * 办公区 Service 实现类
 */
@Service
@Validated
public class OfficeAreaServiceImpl implements OfficeAreaService {

    @Resource
    private OfficeAreaRepository officeAreaRepository;




    @Override
    public PageResult<OfficeAreaPageQueryOutput> pageQuery(OfficeAreaPageQueryInput inputVO) {
        Page<OfficeArea> pageOfficeArea = officeAreaRepository.pageQuery(inputVO);
        List<OfficeAreaPageQueryOutput> listOfficeArea = OfficeAreaConvert.INSTANCE.pageQueryOutputConvert(pageOfficeArea.getRows());
        return new PageResult<>(listOfficeArea, pageOfficeArea.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(OfficeAreaCreateInput inputVO) {
        OfficeArea newOfficeArea = OfficeAreaConvert.INSTANCE.createInputConvert(inputVO);
        newOfficeArea = officeAreaRepository.insert(newOfficeArea);
        return newOfficeArea.id();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(OfficeAreaUpdateInput inputVO) {
        Optional<OfficeArea> optionalOldOfficeArea = officeAreaRepository.findById(inputVO.getId());
        if(!optionalOldOfficeArea.isPresent()){
                throw exception(OFFICE_AREA_NOT_EXIST);
        }
        OfficeArea updateOfficeArea = OfficeAreaConvert.INSTANCE.updateInputConvert(inputVO);
        if (!EntityUtils.isEquals(optionalOldOfficeArea.get(), updateOfficeArea))
            officeAreaRepository.update(updateOfficeArea);
        return true;
    }

    @Override
    public OfficeAreaSingleGetOutput singleGet(Long id) {
        Optional<OfficeArea> optionOfficeArea = officeAreaRepository.findById(id);
        if(!optionOfficeArea.isPresent()){
            throw exception(OFFICE_AREA_NOT_EXIST);
        }
        return OfficeAreaConvert.INSTANCE.singleGetOutputConvert(optionOfficeArea.get());

    }

    @Override
    public List<OfficeAreaSimpleListOutput> simpleList() {
        return OfficeAreaConvert.INSTANCE.simpleListOutputConvert(officeAreaRepository.findAll());
    }

}
